package com.ibangnuo.manage.service;

import com.ibangnuo.framework.domain.Permission;
import com.ibangnuo.framework.domain.Role;
import com.ibangnuo.framework.domain.ext.MenuExt;
import com.ibangnuo.framework.model.response.CommonCode;
import com.ibangnuo.framework.model.response.QueryResponseResult;
import com.ibangnuo.framework.model.response.QueryResult;
import com.ibangnuo.manage.dao.PermissionMapper;
import com.ibangnuo.manage.dao.RoleMapper;
import com.ibangnuo.manage.dao.RoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Author:ly
 * @Data:2020/1/9 14:09
 * @Verdion:1.0
 */
@Service
public class PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleRepository roleRepository;

    // 查询权限
    public QueryResponseResult findList() {
        List<MenuExt> list = permissionMapper.findList();
        System.out.println(list);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        System.out.println(queryResult);
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //查询角色
    public QueryResponseResult findRoleList(Role role) {
        List<Map> list = roleMapper.findRoleList(role);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //添加角色
    public QueryResponseResult addRole(Role role) {
        Role r = roleRepository.findByRoleCode(role.getRoleCode());
        String id = UUID.randomUUID().toString().replace("-", "");
        if (r == null) {
            role.setId(id);
            role.setCreateTime(new Date());
            role.setRoleCode(role.getRoleCode());
            roleRepository.save(role);
            return new QueryResponseResult(CommonCode.SUCCESS, null);
        }
        return new QueryResponseResult(CommonCode.FAIL, null);
    }

    //查询权限
    public QueryResponseResult findMenuList() {
        List<Map> all = permissionMapper.findAll();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(all);
        queryResult.setTotal(all.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //权限添加
    public QueryResponseResult selectMenu(String id) {
        List<Map> all = permissionMapper.selectMenu(id);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(all);
        queryResult.setTotal(all.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    /*
     * 根据roleId查询
     * 有，就更新
     * 无，就添加
     * */
    public QueryResponseResult editMenu(Map<String, String> map) {
        String roleId = map.get("id");
        List<Permission> p = permissionMapper.findByRoleId(roleId);
        Set<String> set = map.keySet();
        List<String> list = new ArrayList<>();
        set.remove("id");
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        if (p != null) {
            permissionMapper.delete(roleId);
        }
        permissionMapper.editMenu(roleId, list);
        return new QueryResponseResult(CommonCode.SUCCESS, null);
    }

    //角色的删除
    @Transactional
    public QueryResponseResult delRole(String id) {
        List<Permission> p = permissionMapper.findByRoleId(id);
        if (p != null) {
            permissionMapper.delete(id);
        }
        roleMapper.delete(id);
        return new QueryResponseResult(CommonCode.SUCCESS, null);
    }

    //角色信息回显
    public QueryResponseResult findRole(String id) {
        List<Map> list = roleMapper.findRole(id);
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);
    }

    //编辑角色数据信息回显
    public QueryResponseResult editRole(Role role) {
        String roleName = role.getRoleName();
        Role byRoleName = roleRepository.findByRoleCode(role.getRoleCode());
        if (byRoleName != null) {
            return new QueryResponseResult(CommonCode.FAIL, null);
        }
        byRoleName.setRoleName(roleName);
        byRoleName.setUpdateTime(new Date());
        byRoleName.setRoleCode(role.getRoleCode());
        Role save = roleRepository.save(byRoleName);
        return new QueryResponseResult(CommonCode.SUCCESS, null);
    }

    //角色查询
    public QueryResponseResult findRoleName() {

        List<Map> list = roleMapper.findRoleName();
        QueryResult queryResult = new QueryResult();
        queryResult.setList(list);
        queryResult.setTotal(list.size());
        return new QueryResponseResult(CommonCode.SUCCESS, queryResult);

    }

    //角色编辑
    public QueryResponseResult upRole(Role role) {
        Role byRoleName = roleRepository.findById(role.getId()).get();
        Role byRoleName1 = roleRepository.findByRoleName(role.getRoleName());
        if (byRoleName1 == null) {
            role.setRoleName(role.getRoleName());
            role.setUpdateTime(new Date());
            role.setRoleCode(byRoleName.getRoleCode());
            role.setId(byRoleName.getId());
//            roleMapper.upRole(role);
            roleRepository.save(role);
            return new QueryResponseResult(CommonCode.SUCCESS, null);
        }
        return new QueryResponseResult(CommonCode.FAIL, null);
    }
//查询所有的角色名称
    public QueryResponseResult findRoleNameAll() {
        List<Role> all = roleRepository.findAll();
        QueryResult queryResult=new QueryResult();
        queryResult.setList(all);
        queryResult.setTotal(all.size());
        return new QueryResponseResult(CommonCode.FAIL, queryResult);


    }
}
